import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart';
import 'package:yuwei_study_language_tools_flutter/widget/subtitle_widget.dart';

class StudyScreen extends StatefulWidget {
  StudyScreen(
      {super.key,
      required this.mode,
      this.mp4Path,
      this.srtPath1,
      this.srtPath2});

  // 运行模式
  int mode = 0;

  // mp4路径
  String? mp4Path;

  // srt路径
  String? srtPath1;
  String? srtPath2;

  @override
  State<StudyScreen> createState() => _StudyScreenState();
}

class _StudyScreenState extends State<StudyScreen> {
  late final player = Player();
  late final videoController = VideoController(player);

  String curSubtitle1 = "test";
  String curSubtitle2 = "测试";

  bool showSubtitle1 = true;
  bool showSubtitle2 = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          // 播放器
          Expanded(child: buildMoviePanel()),
          // 字幕
          // buildSubtitlePanel(),
          // 工具栏
          buildToolbar(),
          // 学习进度
          // buildProgressBar(),
          // 输入栏
          // buildInputPanel()
        ],
      ),
    );
  }

  buildMoviePanel() {
    return Video(
      controller: videoController,
    );
  }

  buildSubtitlePanel() {
    List<Widget> l = [];
    if (showSubtitle1! && widget.srtPath1 != null) {
      l.add(SubtitleWidget(
        subtitle: curSubtitle1,
      ));
    }
    if (showSubtitle2! && widget.srtPath2 != null) {
      l.add(SubtitleWidget(
        subtitle: curSubtitle2,
      ));
    }
    return Column(
      children: l,
    );
  }

  buildToolbar() {
    return Container(
      height: 40,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          OutlinedButton(
              onPressed: () {
                op("play");
              },
              child: Text("播放")),
        ],
      ),
    );
  }

  buildProgressBar() {
    return Placeholder();
  }

  buildInputPanel() {
    return Placeholder();
  }

  @override
  void initState() {
    super.initState();

    playMp4();
  }

  void op(String op) {
    print("执行操作${op}");
    switch (op) {
      case "play":
        playMp4();
        break;
    }
  }

  void playMp4() {
    print("播放器视频:");
    print(widget.mp4Path!);
    player.open(Media(widget.mp4Path!));
  }
}
